package org.springblade.pm.wrappper;

import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.pm.entity.XmglProject;
import org.springblade.pm.service.IXmglProjectService;
import org.springblade.pm.vo.XmglProjectVo;
import org.springblade.system.feign.IDictClient;
import org.springblade.system.feign.ISysClient;
import org.springblade.system.user.entity.UserInfo;
import org.springblade.system.user.feign.IUserClient;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author LK
 * @version 1.0.0
 * @Title: XmglProjectWrapper.java
 * @Description: TODO
 * @date 2020/3/23 15:32
 */
public class XmglProjectWrapper extends BaseEntityWrapper<XmglProject, XmglProjectVo> {

	static ISysClient sysClient;
	static IDictClient dictClient;
	static IUserClient userClient;
	static IXmglProjectService xmglProjectService;

	static {

		sysClient = SpringUtil.getBean(ISysClient.class);
		dictClient = SpringUtil.getBean(IDictClient.class);
		userClient = SpringUtil.getBean(IUserClient.class);
		xmglProjectService = SpringUtil.getBean(IXmglProjectService.class);
	}

	public static XmglProjectWrapper build() {
		return new XmglProjectWrapper();
	}

	@Override
	public XmglProjectVo entityVO(XmglProject entity) {
		XmglProjectVo xmglProjectVo = BeanUtil.copy(entity, XmglProjectVo.class);
		R<String> projectStatusName = dictClient.getValue("project_stutas", entity.getProjectStatus());
		//设置项目状态名称
		if (projectStatusName.isSuccess()) {
			xmglProjectVo.setProjectStatusName(projectStatusName.getData());
		}
		//设置当前状态名称
		R<String> currentStage = dictClient.getValue("project_stage", entity.getCurrentStage());
		if (currentStage.isSuccess()) {
			xmglProjectVo.setCurrentStageName(currentStage.getData());
		}
		if (entity.getLeader() != null) {
			R<UserInfo> userInfo = userClient.userInfo(Long.parseLong(entity.getLeader()));
			if (userInfo.isSuccess()) {
				xmglProjectVo.setLeaderName(userInfo.getData().getUser().getRealName());
			}
		}
		/*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		if(xmglProjectVo.getScheduledEndTime()!=null){
			xmglProjectVo.setEnd(simpleDateFormat.format(xmglProjectVo.getScheduledEndTime()));
		}*/
		//设置报销单位
		if (entity.getGsid() != null) {
			R<String> ssgs = dictClient.getValue("bx_company", Integer.parseInt(entity.getGsid()));
			if (ssgs.isSuccess()) {
				xmglProjectVo.setSsgs(ssgs.getData());
			}
		}
		//设置部门名称
		if (entity.getDeptId() != null) {
			xmglProjectVo.setDeptName(sysClient.getDeptName(Integer.parseInt(entity.getDeptId())));
		}

		//设置开发模式
		/*if (entity.getDevelopPattern() != null) {
			R<String> ssgs = dictClient.getValue("develop_pattern", Integer.parseInt(entity.getDevelopPattern()));
			if (ssgs.isSuccess()) {
				xmglProjectVo.setDevelopPattern(ssgs.getData());
			}
		}*/

		R<String> developPatternName = dictClient.getValue("develop_pattern", entity.getDevelopPattern());
		if (developPatternName.isSuccess()) {
			xmglProjectVo.setDevelopPatternName(developPatternName.getData());
		}

		R<String> modelName = dictClient.getValue("project_model", entity.getModel());
		if (modelName.isSuccess()) {
			xmglProjectVo.setModelName(modelName.getData());
		}
		//是否有合同
		R<String> isContract = dictClient.getValue("yes_or_no", entity.getIsContract());
		if (isContract.isSuccess()) {
			xmglProjectVo.setIsContractName(isContract.getData());
		}
		//是否有
		R<String> isAssociation = dictClient.getValue("yes_or_no", entity.getIsAssociation());
		if (isAssociation.isSuccess()) {
			xmglProjectVo.setIsAssociationName(isAssociation.getData());
		}

		//避免Integer类型为null前端默认返回-1的
		if (entity.getBudgetManpower()==null){
			xmglProjectVo.setBudgetManpowerString("");
		}else{
			xmglProjectVo.setBudgetManpowerString(entity.getBudgetManpower().toString());
		}
		if (entity.getBudgetAmount()==null||"0.00".equals(entity.getBudgetAmount().toString())){
			xmglProjectVo.setBudgetAmount(new BigDecimal(0));
		}else {
			xmglProjectVo.setBudgetAmountString(entity.getBudgetAmount().toString());
		}

		return xmglProjectVo;
	}

	public List<XmglProjectVo> entityVOList(List<XmglProject> list) {
		List<XmglProjectVo> xmglProjectVoList = new ArrayList<>();
		for (XmglProject xmglProject : list) {
			xmglProjectVoList.add(entityVO(xmglProject));
		}
		return xmglProjectVoList;
	}
}
